<?php

class YahooController extends Zend_Controller_Action
{
	function init() {
		$this->getHelper('viewRenderer')->setNoRender();
		$this->_helper->layout->disableLayout();
		
	}

	//we redirect the user to google for authentication
	function indexAction(){
		try{
			$auth_request = Myzen_Openid::getAuthRequest('yahoo');
		} catch(Myzen_Exception $e) {
			$this->view->messageDisplay('An issue occured while connecting to yahoo, please retry later','error');
			$this->_redirect($this->view->baseUrl().'/auth/login/signup/');
			return;			
		} //end catch
		if ($auth_request->shouldSendRedirect()) {
			$redirect_url = $auth_request->redirectURL(Myzen_Openid::getTrustRoot(),Myzen_Openid::getReturnTo('yahoo'));
			if (Auth_OpenID::isFailure($redirect_url)) {
				$this->view->messageDisplay('An issue occured while connecting to yahoo, please retry later','error');
				$this->_redirect($this->view->baseUrl().'/auth/login/signup/');
				return;
        	} else {
            // Send redirect.
            header("Location: ".$redirect_url);
        }
      } else {
        // Generate form markup and render it.
        $form_html = $auth_request->htmlMarkup(Myzen_Openid::getTrustRoot(), Myzen_Openid::getReturnTo('yahoo'),false, array('id' => 'openid_message'));
        if (Auth_OpenID::isFailure($form_html)) {
        		$this->view->messageDisplay('An issue occured while connecting to yahoo, please retry later','error');
				$this->_redirect($this->view->baseUrl().'/auth/login/signup/');
				return;
            throw new Exception('Could not redirect to server: '.$form_html->message);
        } else {
				$search = array(
					'/<\/form>/',
					'/<\/head>/'
				);
				$replace = array(
					'</form><div id="page"><div id="main"><div id="pendingYahoo">Connecting to Yahoo</div></div></div>',
					'<link href="/meeting/zend/public/ressources/style/default/style.css" media="screen" rel="stylesheet" type="text/css" /></head>'
				);
            echo preg_replace($search,$replace,$form_html);
        }
      }//end else
    
	} //end indexAction
	
	//when the provider returns the authenticated user, the rest is performed here
	function completeauthAction() {
		$consumer = Myzen_Openid::getConsumer();
    	$return_to = Myzen_Openid::getReturnTo('yahoo');
    	$response = $consumer->complete(Myzen_Openid::getReturnTo('yahoo'));

    	// Check the response status.
		if ($response->status == Auth_OpenID_CANCEL) {
			$this->view->messageDisplay('You need to accept the request in order to login','error');
			$this->_redirect($this->view->baseUrl().'/auth/login/signup/');
			return;
		} else if ($response->status == Auth_OpenID_FAILURE) {
			$this->view->messageDisplay('An issue occured while identifying you, please retry later','error');
			$this->_redirect($this->view->baseUrl().'/auth/login/signup/');
			return;
		} else if ($response->status == Auth_OpenID_SUCCESS) {
			$details = Myzen_Openid::getUserDetails('yahoo',$response);
			$this->view->assign($details);
			$this->_forward('completeauthfromexternalprovider','index','auth');
			return;
		} //end authentication ok	

		//we should not be here so we log the status that was not caught
		$this->view->messageDisplay('An issue occured while identifying you, please retry later','error');
		$this->_redirect($this->view->baseUrl().'/auth/login/signup/');
		return;		
	} //end completeauth 	
	
	
} //end class